import os
import Image
import numpy

from OpenGL.GL import *

def file_path(file):
	"""
	Returns a relative path to a file.
	"""
	
	return os.path.join(os.path.dirname(__file__), file)

def bind_texture(file):
	tex_id = glGenTextures(1)
	
	img = Image.open(file_path(file))
	imgdata = numpy.array(list(img.getdata()), numpy.int8)
	
	glBindTexture(GL_TEXTURE_2D, tex_id)
	glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[0],
				 0, GL_RGB, GL_UNSIGNED_BYTE, imgdata)
	
	return tex_id